<?php
session_start();
//生成验证码图片
header("Content-type: image/png");
// 全数字
//test:abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789
// $str = "a,b,c,d,e,f,g,h,k,m,n,p,r,s,t,u,v,w,x,y,z,1,2,3,4,5,6,7,8,9";      //要显示的字符，可自己进行增删
$str = "0,1,2,3,4,5,6,7,8,9";   
$list = explode(",", $str);
$cmax = count($list) - 1;
$verifyCode = '';
for ( $i=0; $i < 4; $i++ ){
      $randnum = mt_rand(0, $cmax);
      $verifyCode .= $list[$randnum];           //取出字符，组合成为我们要的验证码字符
}
$_SESSION['code'] = $verifyCode;        //将字符放入SESSION中
 
$im = imagecreate(90,38);    //生成图片$im = imagecreate(50,20); 
$black = imagecolorallocate($im, 0,0,0);     //此条及以下三条为设置的颜色
$white = imagecolorallocate($im, 255,255,255);
$gray = imagecolorallocate($im, 200,200,200);
$red = imagecolorallocate($im, 255, 0, 0);
$testcolor = imagecolorallocate($im, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); 
$fontcolor = imagecolorallocate($im,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagefill($im,0,0,$testcolor);     //给图片填充颜色
 
//将验证码绘入图片(ldhz项目)
//imagestring($im, 5, 3, 2, $verifyCode, $fontcolor);    //将验证码写入到图片中
//将验证码绘入图片(test项目)
 
$width=90;$height=38;$fontsize = 20;$font='../public/font/Jura.ttf';
//imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
$_x = $width / 4;
for ($i=0;$i<4;$i++) {  
    $fontcolor = imagecolorallocate($im,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));  
    imagettftext($im,$fontsize,mt_rand(-30,30),$_x*$i+mt_rand(2,5),$height/1.4,$fontcolor,$font,$verifyCode[$i]);  
}
/**/
for($i=0;$i<4;$i++)  //加入干扰象素
{
    imagesetpixel($im, rand(0,99) , rand(0,99) , $black);    //加入点状干扰素
    imagesetpixel($im, rand(0,99) , rand(0,99) , $red);
    imagesetpixel($im, rand(0,99) , rand(0,99) , $gray);
    imagearc($im, rand(), rand(), 90, 20, 75, 170, $black);    //加入弧线状干扰素
    imageline($im, rand(), rand(), rand(), rand(), $red);    //加入线条状干扰素
}
imagepng($im);
imagedestroy($im);